import { 
  GET_DATA, 
  GET_DATA_SUCCESS, 
  GET_DATA_FAIL,
  DELETE_TEMPLATE,
  DELETE_TEMPLATE_SUCCESS,
  DELETE_TEMPLATE_FAIL,
  SET_TEMPLATE,
  SET_STATUS,
  SET_START_TIME,
  SET_END_TIME,
  SET_PAGE_NUM,
} from '@/actions/template'
import util from '@/utils';

const initState = {
  data: [],
  pageNum: 1,
  pageSize: 10,
  total: 0,
  template: '',
  status: null,
  startTime: null,
  endTime: null,
  loading: false,
}

const templateReducer = (state = initState, action) => {
  switch (action.type) {
    case GET_DATA:
      return { ...state, loading: true }
      break;

    case GET_DATA_SUCCESS:
      const { data:{ list, total }  } = action
      return { ...state, data: list, total, loading: false }
      break;

    case GET_DATA_FAIL:
      return { ...state, loading: false }
      break;  

    case SET_TEMPLATE:
      return { ...state, template: action.template }
      break;

    case SET_STATUS:
      return { ...state, status: action.status }
      break;

    case SET_START_TIME:
      return { ...state, startTime: util.getStartTime(action.startTime) }
      break;

    case SET_END_TIME:
      return { ...state, endTime: util.getEndTime(action.endTime) }
      break;

    case SET_PAGE_NUM:
      return { ...state, pageNum: action.pageNum }
      break;

    case DELETE_TEMPLATE:
      return { ...state, loading: true }
      break;

    case DELETE_TEMPLATE_SUCCESS:
    case DELETE_TEMPLATE_FAIL:
      return { ...state, loading: false }
      break;

    default:
      return state
      break;
  }
}

export default templateReducer